package com.wjk.blogs.web.admin;

import com.wjk.blogs.po.Tag;
import com.wjk.blogs.service.BlogService;
import com.wjk.blogs.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping("/admin")
public class TagController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    @GetMapping("/tags")
    public String tags(
            @PageableDefault(
                    size= 10
                    ,sort={"id"}
                    ,direction = Sort.Direction.DESC
            ) Pageable pageable
            , Model model
    ){
        model.addAttribute("page",
                tagService.listType(pageable)
        );
        return "admin/tags";
    }

    @GetMapping("/tags/input")
    public String input(Model model){
        model.addAttribute("tag",new Tag());
        return "admin/tags-input";
    }

    @PostMapping("/tags")
    //@Valid和BindingResult配套使用，@Valid用在参数前，BindingResult作为校验结果绑定返回
    public String post(@Valid Tag tag
            , BindingResult result
            , RedirectAttributes attributes){

        //添加或者编辑时确保数据库中没有重复数据
        Tag tag1=tagService.getTagByName(tag.getName());

        //后端验证是否重复
        if (tag1!=null){
            result.rejectValue("name"
                    ,"nameError"
                    ,"不能添加重复的分类");
        }

        //校验是否通过,如果通过了
        if (result.hasErrors()){
            return "admin/tags-input";
        }

        //向数据库中保存数据
        Tag t=tagService.saveTag(tag);

        //向页面提示
        if (t == null){
            //提示保存失败
            attributes.addFlashAttribute("message","操作失败");
        }else{
            //提示保存成功
            attributes.addFlashAttribute("message","操作成功");
        }

        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/input")
    /*通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过*/
    public String editinput(@PathVariable Long id, Model model){
        model.addAttribute("tag",tagService.getTag(id));
        return "/admin/tags-input";
    }

    @PostMapping("/tags/{id}")
    public String editPost(
            @Valid Tag tag
            , BindingResult result
            ,@PathVariable Long id
            , RedirectAttributes attributes){

        //检查是否有重复标签
        Tag t=tagService.getTagByName(tag.getName());
        if(t != null){
            result.rejectValue("name","nameError","不能添加重复的标签");
        }
        if (result.hasErrors()){
            return "admin/tags-input";
        }

        //标签更新
        Tag t2=tagService.updateTag(id,tag);
        if (t2==null){
            attributes.addFlashAttribute("message","操作失败");
        }else {
            attributes.addFlashAttribute("message","操作成功");
        }
        return "redirect:/admin/tags";
    }

    @GetMapping("/tags/{id}/delete")
    public String deleteTag(@PathVariable Long id,RedirectAttributes attributes){
        if (blogService.BlogHasTag().contains(tagService.getTag(id).getId().toString())){
            System.out.println("tagService" + tagService.getTag(id).getId().toString());

            attributes.addFlashAttribute("message","删除失败,还有绑定此标签的博客,如要删除此标签,请在博客中取消对标签的绑定");
            return "redirect:/admin/tags";
        }
        tagService.deleteTag(id);
        attributes.addFlashAttribute("message","删除成功");
        return "redirect:/admin/tags";
    }


}
